package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.RedisConst;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * @author zhangmeng
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@DubboService
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public long countByImg(String img) {
        return setMealDao.countByImg(img);
    }

    @Override
    public List<Setmeal> findAll() {
//        redisTemplate.delete(RedisConst.SETMEAL_ALL_LIST);
        //调用DAO查询所有
        //如果redis中有
        if (redisTemplate.hasKey(RedisConst.SETMEAL_ALL_LIST)) {
            //取出数据返回
            List<Setmeal> setmealList = redisTemplate.opsForList().range(RedisConst.SETMEAL_ALL_LIST, 0, -1);
            log.debug("[从Redis中获取到数据:{}]",setmealList);
            return setmealList;
        }
        //如果没有，则从数据库中查询，再放入redis
        List<Setmeal> setmealList = setMealDao.selectAll();
        log.debug("[Redis中找不到数据，去数据库查询]");
        redisTemplate.opsForList().rightPushAll(RedisConst.SETMEAL_ALL_LIST,setmealList);
        //设置数据超时时间
//        this.redisTemplate.expire(RedisConst.SETMEAL_ALL_LIST,1L, TimeUnit.MINUTES);
        log.debug("[将从数据库查询到的数据存入Redis:{}]",setmealList);

        return setmealList;

    }

    @Override
    public Setmeal findById(Integer id) {
        HashOperations hash = redisTemplate.opsForHash();
        //调用DAO查询数据
        if (redisTemplate.hasKey(RedisConst.SETMEAL_ID_DETAILS)){
            Setmeal setmealDetails = (Setmeal) hash.get(RedisConst.SETMEAL_ID_DETAILS,id);
            log.debug("[redis中数据是:{}]",setmealDetails);
            if (null != setmealDetails){
                return setmealDetails;
            }
        }
        //没查到对应的数据,去数据库查
        Setmeal setmealDetails = setMealDao.selectById(id);
        //放入Redis
        hash.put(RedisConst.SETMEAL_ID_DETAILS,id,setmealDetails);
        log.debug("[Redis中没有数据，从数据库中查询]");
//        this.redisTemplate.expire(RedisConst.SETMEAL_ID_DETAILS,1L,TimeUnit.MINUTES);
        log.debug("[将数据存入到Redis] id:{},setmeal:{}",id,setmealDetails);
        return setmealDetails;
    }
}
